<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
请输入数字：<input type="text" id="num" onkeyup="show('num')">&nbsp;<span id="msg"></span><br><br>
<input type="button" value="计算平方值" onclick="show('num')"><br><br>
<script>
    function pf(i){
        return i*i;
    }

    function show(id){
        //var n = parseFloat(document.getElementById(id).value);
        if(isNaN(document.getElementById(id).value)){
            document.getElementById('msg').innerHTML = '请填写数字';
            return;
        }

        var n = parseInt(document.getElementById(id).value);
        //alert(n*n);

        document.getElementById('msg').innerHTML = n*n;
    }
    //document.write(pf(21));
</script>
</body>
</html>